package com.masterclient.app.businessmodule.minemodule.collection.contract;

import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselibrary.bean.goods.ProductBean;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselibrary.bean.personal.GoodsBean;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2017/07/24
*/
public class GoodsListPresenter extends GoodsListContract.Presenter{

    public List<ProductBean> list;

    //TODO:此方法判断是否是下拉刷新，例外判断是否有数据源（主要控制loading和默认页面显示）
    @Override
    public void getCollectGoodsList(String page, String page_size,boolean isRefresh) {

        if (!isHasData() && !isRefresh) {
            mView.baseShowPageLoading(false);
        }

        Observable<GoodsBean> observable = RetrofitUtil.getInstance().getCollectGoodsList(page,page_size);

        Disposable disposable = toSubscribe(observable, new Consumer<GoodsBean>() {
            @Override
            public void accept(@NonNull GoodsBean bean) throws Exception {
                mView.baseHiddenPageLoading();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                if(bean!=null && bean.getData()!=null){
                    mView.showCollectGoodsList(bean.getData());
                } else {
                    ToastUtil.showToast(SERVER_FAILED_STR);
                    mView.baseShowError();
                    if (!isHasData()) {
                        mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                    }
                }
            }
        },  new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.baseHiddenPageLoading();
                ToastUtil.showToast(WEB_FAILED_STR);
                mView.baseShowError();
                if (!isHasData()) {
                    mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                }
            }
        });

        mRxManager.add(disposable);
    }

    public boolean isHasData() {
        if (list != null && list.size() > 0) {
            return true;
        }
        return false;
    }


}